package com.cookor.poi.modules.phy.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.phy.entity.TBookNo;
import com.cookor.poi.modules.phy.service.TBookNoService;
import com.cookor.poi.modules.sys.utils.UserUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/phy/tBookNo"})
public class TBookNoController extends BaseController {
  @Autowired
  private TBookNoService tBookNoService;
  
  @ModelAttribute
  public TBookNo get(@RequestParam(required = false) String id) {
    TBookNo entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.tBookNoService.get(id); 
    if (entity == null)
      entity = new TBookNo(); 
    return entity;
  }
  
  @RequiresPermissions({"phy:tBookNo:view"})
  @RequestMapping({"list", ""})
  public String list(TBookNo tBookNo, HttpServletRequest request, HttpServletResponse response, Model model) {
    tBookNo.setTenantId(UserUtils.getTenantId());
    Page<TBookNo> page = this.tBookNoService.findPage(new Page(request, response), tBookNo);
    model.addAttribute("page", page);
    return "modules/phy/tBookNoList";
  }
  
  @RequiresPermissions({"phy:tBookNo:view"})
  @RequestMapping({"form"})
  public String form(TBookNo tBookNo, Model model) {
    model.addAttribute("tBookNo", tBookNo);
    model.addAttribute("admin", UserUtils.getUser());
    return "modules/phy/tBookNoForm";
  }
  
  @RequiresPermissions({"phy:tBookNo:edit"})
  @RequestMapping({"save"})
  public String save(TBookNo tBookNo, Model model, RedirectAttributes redirectAttributes) {
    if (!beanValidator(model, tBookNo, new Class[0]))
      return form(tBookNo, model); 
    this.tBookNoService.save(tBookNo);
    addMessage(redirectAttributes, new String[] { "保存预约号成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tBookNo/?repage";
  }
  
  @RequiresPermissions({"phy:tBookNo:edit"})
  @RequestMapping({"delete"})
  public String delete(TBookNo tBookNo, RedirectAttributes redirectAttributes) {
    this.tBookNoService.delete(tBookNo);
    addMessage(redirectAttributes, new String[] { "删除预约号成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tBookNo/?repage";
  }
}
